/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.findcustomer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 联系记录Controller
 * @author 方志钧
 * @version 2016-12-07
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/findcustomer/allRecords")
public class AllRecordsController extends BaseController {

	@Autowired
	private AllRecordsService allRecordsService;
	
	@ModelAttribute
	public AllRecords get(@RequestParam(required=false) String id) {
		AllRecords entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = allRecordsService.get(id);
		}
		if (entity == null){
			entity = new AllRecords();
		}
		return entity;
	}
	
	/*@RequiresPermissions("findcustomer:allRecords:view")*/
	@RequestMapping(value = {"list", ""})
	public String list(AllRecords allRecords, HttpServletRequest request, HttpServletResponse response, Model model) {
		allRecords.setCustomerId(request.getParameter("customerId"));
		Page<AllRecords> page = allRecordsService.findRecordPage(new Page<AllRecords>(request, response), allRecords); 
		model.addAttribute("page", page);
		return "modules/crm/findcustomer/allRecordsList";
	}

	@RequiresPermissions("findcustomer:allRecords:view")
	@RequestMapping(value = "form")
	public String form(AllRecords allRecords, Model model) {
		model.addAttribute("allRecords", allRecords);
		return "modules/crm/findcustomer/allRecordsForm";
	}

	@RequiresPermissions("findcustomer:allRecords:edit")
	@RequestMapping(value = "save")
	public String save(AllRecords allRecords, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, allRecords)){
			return form(allRecords, model);
		}
		allRecordsService.save(allRecords);
		addMessage(redirectAttributes, "保存联系记录成功");
		return "redirect:"+Global.getAdminPath()+"/crm/findcustomer/allRecords/?repage";
	}
	
	@RequiresPermissions("findcustomer:allRecords:edit")
	@RequestMapping(value = "delete")
	public String delete(AllRecords allRecords, RedirectAttributes redirectAttributes) {
		allRecordsService.delete(allRecords);
		addMessage(redirectAttributes, "删除联系记录成功");
		return "redirect:"+Global.getAdminPath()+"/crm/findcustomer/allRecords/?repage";
	}

}